﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;
using BAL.Controller;

namespace PresentationLayer.View.DepartmentManager
{
    public partial class AssignDepartmentRepresentative : System.Web.UI.Page
    {
               List<CollectionPoint> cp_list;
               private static List<Employee> depEmp_list;
               private static List<Employee> empe_list;
               private static List<Department> dep_list;
               private static bool flag;
               Employee loginDepHead;
      
         protected void Page_Load(object sender, EventArgs e)
         {   
          
            if (!IsPostBack)
            {
                loginDepHead = new Employee();
           
                loginDepHead = (Employee)Session["CurrentUser"];
               
                
                /*Current Collection Point*/
                CollectionPoint c_Obj = new CollectionPoint();
                cp_list = new List<CollectionPoint>();
                c_Obj = DelRepresentativeController.getDelRepAuthorityController().getCollectionPointByDeptId(loginDepHead.DeptId.ToString());
                if (c_Obj != null)
                {
                    lblCollectionPoint.Text = c_Obj.Place.ToString();
                }
                else lblCollectionPoint.Text = "NIL";
                
                
                /*Current Representative*/
                Employee currentRep = new Employee();
                currentRep = DelRepresentativeController.getDelRepAuthorityController().getEmployeebyDeptIdAndRole(loginDepHead.DeptId.ToString(), 3);
                if (currentRep != null)
                {
                    lblCurrentRep.Text = currentRep.EmpName.ToString();
                }
                else lblCurrentRep.Text = "NIL";
                ViewState["curRep"] = currentRep;

               
                /*Display Collection Point in DropDownList*/
                cp_list = DelRepresentativeController.getDelRepAuthorityController().selectAllCollectionPoint();
                ddlNewColPoint.DataSource = cp_list;
                ddlNewColPoint.DataTextField = "Place";
                ddlNewColPoint.DataValueField = "CollectionId";
                ddlNewColPoint.DataBind();
          

               
                empe_list = new List<Employee>();
                depEmp_list = new List<Employee>();
                dep_list = new List<Department>();
               
                // **** respective Department Employee list display in GridView *************

                dep_list = DelRepresentativeController.getDelRepAuthorityController().selectAllDepartment();
                depEmp_list = DelRepresentativeController.getDelRepAuthorityController().selectEmployeeByDept(loginDepHead.DeptId.ToString());
                empe_list = DelRepresentativeController.getDelRepAuthorityController().getEmployeeListByNotIncludeEmpIdAndRoleId(loginDepHead.DeptId.ToString(), loginDepHead.EmpId.ToString(), 3);
             
            }

                if (flag != true)
               {
                   Bind(empe_list);
               }   else flag = false;
            
         
         }

         protected void Bind(List<Employee> empe_list)
         {
             if (empe_list.Count == 0)
             {
                 GVEmpList.DataSource = null;
                 GVEmpList.DataBind();
             }
             else
             {
                 GVEmpList.DataSource = empe_list;
                 GVEmpList.DataBind();
             }
         }

         protected void btnSearch_Click(object sender, EventArgs e)
         {
                 Employee empe = new Employee();
                 empe = (Employee)Session["CurrentUser"];
                 string Depname = empe.DeptId;
                 GVEmpList.DataSource = DelRepresentativeController.getDelRepAuthorityController().getEmployeebyDeptIdAndEmpName(Depname, txtEmployee.Text.Trim()); 
                 GVEmpList.DataBind();
                 flag = true;

         }

     
         protected void btnselect_Click(object sender, EventArgs e)
         {

                  LinkButton EmpIdButton = sender as LinkButton;
                  int employeeid = Convert.ToInt32(EmpIdButton.CommandArgument);
                  EmpIdButton.Enabled = false;
          
                  ViewState["selEmp"] = Convert.ToString(employeeid);
                  lblmessage.Visible = true;
                  lblmessage.Text = "Employee ID '" + ViewState["selEmp"] + "' has been selected.";

          
          }

         protected void btnDelegate_Click(object sender, EventArgs e)
         {
                    string selEmp;
                    string selDep;
                    string crep_id;
              
                    Employee empe = new Employee();

                    empe = (Employee)Session["CurrentUser"];
                    selDep = empe.DeptId.ToString();
               
            
                   /*find Grid row object from empe_List by selected employeeid*/
                   if (ViewState["selEmp"]==null)
                    {
                       lblmessage.Visible = true;
                       lblmessage.Text="Please select employee to assign.";
                    }
                   else
                   {
                           selEmp = Convert.ToString(ViewState["selEmp"]);
                           if (depEmp_list.Count != 0)
                           {
                               Employee myLocatedObj = depEmp_list.Find(delegate(Employee ep) { return ep.EmpId == selEmp; });

                               if (myLocatedObj != null)
                               {
                                  /*Upate DeptRep RoleId "3" for new assigned  person at Employee table*/
                                  myLocatedObj.RoleId = 3;
                                  int result = DelRepresentativeController.getDelRepAuthorityController().updateEmployee(myLocatedObj, selEmp);
                                  Send_email(myLocatedObj, ddlNewColPoint.SelectedValue.ToString());
                               }


                             /*update old Dept Rep to normal Employee role*/
                             Employee crep_Obj = new Employee();
                             if (ViewState["curRep"] != null)
                              {
                                  crep_Obj = (Employee)ViewState["curRep"];
                                  crep_Obj.RoleId = 5;
                                  crep_id = crep_Obj.EmpId.ToString();
                                  int cresult = DelRepresentativeController.getDelRepAuthorityController().updateEmployee(crep_Obj, crep_id);
                                  Send_email(crep_Obj, ddlNewColPoint.SelectedValue.ToString());

                              }
                            }

                           /*update newDeptRep in Department table*/
                            if (dep_list.Count != 0)
                            {
                             
                                 Department myDeptObj = dep_list.Find(delegate(Department dep) { return dep.DeptId == selDep; });
                                 myDeptObj.RepresentativeId = selEmp;
                                 myDeptObj.CollectionId = Convert.ToInt32(ddlNewColPoint.SelectedValue.ToString());
                                 int uresult = DelRepresentativeController.getDelRepAuthorityController().updateDepartment(myDeptObj, selDep);
                                 lblmessage.Text = "Successfully Delegated to Employee ID '" + ViewState["selEmp"] + "'.";
                                 Ipanel.Visible = false;
                             

                            }
                     }

          }


         public void Send_email(Employee eObj, string Collectionpint)
         {
             Email semail = new Email(eObj,Collectionpint);
             semail.createMail();


         }

            //GV Link button Enable/Disable 
         protected void GVEmpList_RowDataBound(object sender, GridViewRowEventArgs e)
         {
               if (e.Row.RowType == DataControlRowType.DataRow)
               {
                   if (e.Row.Cells[1].Text == Convert.ToString(ViewState["selEmp"]))
                   {
                     ((LinkButton)e.Row.FindControl("btnselect")).Enabled = true;
                    }
                }

          }

         protected void btnAll_Click(object sender, EventArgs e)
         {
             Bind(empe_list);
         }

        

    }
}